/**
 * Created by wxm on 2017/8/13.
 */
var SKU_ID = '';
var entryType = 0;
var detailParams = null; //页面所需初始化参数
var settlementParams = {};
var params = {};//下单参数
var SESSION_ID = sessionStorage.getItem("SESSION_ID");
detailParams = JSON.parse(sessionStorage.getItem("skusparams"));
$(document).ready(function () {
    initsettParams(); //初始化商品结算及下单参数
});

//初始化商品结算及下单参数
function initsettParams() {
    //结算&下单
    var SKU_LIST = detailParams.SKU_LIST
    var STORE_SKU_LIST = [{
        "STORE_ID":STORE_ID,
        "SKU_LIST":SKU_LIST
    }];
    var PAYMENT = "WXJSPAY";
    var TOTAL_PRICE = detailParams.totalFee;
    var SOURCE = "WeiXin";
    params = {
        "SESSION_ID":SESSION_ID,
        "STORE_SKU_LIST":STORE_SKU_LIST,
        "PAYMENT":PAYMENT,
        "TOTAL_PRICE":TOTAL_PRICE,
        "SOURCE":SOURCE
    };
    //自提商品结算
    settlementParams = {
        "STORE_SKU_LIST":STORE_SKU_LIST
    }
    if(detailParams.COUPON_NO) {
        settlementParams.COUPON = detailParams.COUPON_NO;
        params.COUPON = detailParams.COUPON_NO;
    }
    productSettlement(settlementParams)
}
//商品结算
function productSettlement() {
    var qpath = 'order/noaddr/settlement';
    request(qpath,settlementParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(!flg) {
            return showErroMsg(res.RSPMSG,function(){
                $(".showErrorMassage").remove();
            });
        }
        var prodItem = '<div class="item-wrap">\
                <div class="prod-item">\
                    <div class="prod-img">\
                        <img src="%s" alt="">\
                    </div>\
                    <div class="prod-name">%s</div>\
                    <div class="prod-number">× <span>%s</span></div>\
                    <div class="price-operate price-selector">\
                        <div class="price red-text"><span>￥</span><span class="sell-price">%s</span><s class="origin-price">%s</s></div>\
                    </div>\
                    </div>\
                </div>';
        var countNum = 0;
        _.each(res.BODY.STORE_SKU_LIST[0].SKU_LIST,function (item, index) {
            countNum += item.SKU_NUM;
            $(".list-contaner").append(sprintf(prodItem,item.PIC,item.NAME,item.SKU_NUM,item.BUY_PRICE,item.LIST_PRICE));
        });
        $("#totalCount").text(countNum);
        $("#totalMoney").text(res.BODY.COUNT_FEE);
        $(".order-total-money").text(res.BODY.COUNT_FEE);
        params.TOTAL_PRICE = res.BODY.COUNT_FEE;
    });
}
//下单支付
$("#pay").on("click",function () {
    var path = "order/noaddr/add";
    request(path,params,function (res) {
        if(entryType == 1) {
            window.location.replace("./order_pay.html?order_id="+res.BODY.ORDER_ID);
        } else {
            //自提咖啡支付
            // alert(JSON.stringify(res));
            var flg = /^0+$/ig.test(res.RSPCD); //全零true
            if(!flg) return showErroMsg("交易失败,返回首页！",function () {
                window.location.replace("./index.html");
            });
            var appId = RegBetween(res.BODY.PAYMENT_STR,"appId=","&timeStamp=");
            var timestamp = RegBetween(res.BODY.PAYMENT_STR,"timeStamp=","&signType=");
            var nonceStr = RegBetween(res.BODY.PAYMENT_STR,"nonceStr=","&paySign=");
            var package = RegBetween(res.BODY.PAYMENT_STR,"package=","&nonceStr=");
            var signType = RegBetween(res.BODY.PAYMENT_STR,"signType=","&package=");
            var paySign = RegBetween(res.BODY.PAYMENT_STR,"paySign=","");
            onBridgeReady(appId,timestamp,nonceStr,package,signType,paySign,function (res1) {
                //跨界下单记录
                var znmakeorderPath = "order/thirdParty/doAdd";
                var anmakeorderParams = {
                    "storeId":STORE_ID,
                    "userId":USER_ID,
                    "orderId":res.BODY.ORDER_ID,
                    "orderSource":1,
                    "operate":1,
                    "skusFee":params.TOTAL_PRICE,
                    "finalPrice":params.TOTAL_PRICE,
                    "skuList":params.STORE_SKU_LIST[0].SKU_LIST
                };
                znRequest(znmakeorderPath,anmakeorderParams,function (res) {
                    alert(JSON.stringify(res));
                });
                // 订单出货
                var shipmentPath = "order/thirdParty/shipment";
                var shipmentParams = {
                    "storeId":STORE_ID,
                    "orderId":res.BODY.ORDER_ID,
                    "orderSource":1,
                    "gatewayNo":machCode
                }
                znRequest(shipmentPath,shipmentParams,function (res) {
                    //
                    location.replace("./order_make.html?order_id="+res.BODY.ORDER_ID);
                });
            });
        }
    });
});